package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserModel;
import service.UserService;
import service.impl.UserServiceImpl;

/**
 * Servlet implementation class ModifyPServlet
 */
@WebServlet("/servlet/ModifyPServlet")
public class ModifyPServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyPServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("username");
		String oldpassword=request.getParameter("oldpassword");
		String password=request.getParameter("password");
		UserModel userModel=new UserModel();
		userModel.setUsername(username);
		userModel.setPassword(password);
		UserService service=new UserServiceImpl();
		UserModel u=null;
		try {
			u=service.modifyP(userModel, oldpassword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(u==null){
			response.sendRedirect("/userlist2.jsp");
		}else{
			request.setAttribute("usermodel",u);	
			request.getRequestDispatcher("/userlist2.jsp").forward(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
